import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_tv_widgets/config/kq_global.dart';
import 'package:kq_flutter_tv_widgets/resources/images.dart';
import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';
import '../../theme/kq_theme_manager.dart';
import '../button/kq_outline_button.dart';

class KqReasonDialog extends Dialog {
  //标题
  final String title;

  ///每一项的
  final String Function(int index)? renderTitleCallback;

  ///每一项的ID
  final String Function(int index)? renderIdCallback;

  ///是否是单选
  final bool isOnlySelect;

  ///选中的,逗号隔开发字符串
  final String? chosenDataId;

  /// 个数
  final int? totalCount;

  ///原因选择区域高度，若不传则自适应
  final double? reasonHeight;

  //点击了哪一项
  final Function(int index)? callBack;

  ///是否显示底部按钮
  final bool? isShowBottomBtn;

  /// 左边按钮文字，默认"取消"
  final String? leftBtnText;

  /// 左边按钮文字，默认"确定"
  final String? rightBtnText;

  /// 左边按钮回调
  final Function()? onLeftBtnTap;

  /// 右边按钮回调
  final Function()? onRightBtnTap;

  ///底部自定义内容
  final Widget? bottomChildWidget;

  const KqReasonDialog(
      {super.key,
      required this.title,
      required this.renderTitleCallback,
      required this.totalCount,
      this.reasonHeight,
      this.isOnlySelect = true,
      this.leftBtnText,
      this.isShowBottomBtn,
      this.rightBtnText,
      this.onLeftBtnTap,
      this.onRightBtnTap,
      this.renderIdCallback,
      this.bottomChildWidget,
      this.chosenDataId,
      this.callBack});

  Widget _gridViewWidget() {
    int total = totalCount!;
    return GridView.builder(
      shrinkWrap: true,
      padding: EdgeInsets.symmetric(horizontal: 32.r),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 5,
        crossAxisSpacing: 16.r,
        mainAxisSpacing: 16.r,
        childAspectRatio: 17 / 7.0,
      ),
      itemCount: total,
      itemBuilder: (BuildContext context, int index) {
        return _renderItemWidget(index);
      },
    );
  }

  Widget _renderItemWidget(int index) {
    String text = renderTitleCallback!(index);
    String? itemId;
    if (renderIdCallback != null) {
      itemId = renderIdCallback!(index);
    }
    String? chosenId = chosenDataId;
    Color bgColor = KqThemeColors.bgBlack05;
    Color txtColor =
        KqThemeManager.instance.getConfig().dialogConfig.titleColor ??
            KqThemeColors.textBlack65;
    if (isOnlySelect) {
      if (itemId != null && chosenId != null && itemId == chosenId) {
        //存在选中
        bgColor = KqThemeColors.bgBlue;
        txtColor = KqThemeColors.textWhite;
      }
    } else {
      if (itemId != null && chosenId != null) {
        List<String> chosenIdList = chosenId.split(',');
        if (chosenIdList.isNotEmpty && chosenIdList.contains(itemId)) {
          bgColor = KqThemeColors.bgBlue;
          txtColor = KqThemeColors.textWhite;
        }
      }
    }

    return InkWell(
      onTap: () {
        if (callBack != null) {
          callBack!(index);
        }
      },
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(2.r),
          color: bgColor,
        ),
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(
          horizontal: 8.r,
        ),
        child: Text(
          text,
          maxLines: 2,
          textAlign: TextAlign.center,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(color: txtColor, fontSize: 24.sp, height: 1.4),
        ),
      ),
    );
  }

  Widget _getBottomBtnList() {
    return Padding(
      padding: EdgeInsets.only(left: 24.r, right: 24, top: 20.r),
      child: Row(
        children: [
          Expanded(
              child: KqOutlineButton.normal(
            title: leftBtnText ?? KqIntl.currentResource.cancel,
            verticalPadding: 22.r,
            onTap: (disabled) {
              if (onLeftBtnTap != null) {
                onLeftBtnTap!();
              }
            },
          )),
          SizedBox(
            width: 24.r,
          ),
          Expanded(
              child: KqOutlineButton(
            title: rightBtnText ?? KqIntl.currentResource.confirm,
            verticalPadding: 22.r,
            onTap: (disabled) {
              if (onRightBtnTap != null) {
                onRightBtnTap!();
              }
            },
          ))
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        width: 1148.r,
        height: 836.r,
        padding: EdgeInsets.only(bottom: 32.r),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(4.r),
          color: KqThemeManager.instance.getConfig().dialogConfig.bgColor ??
              KqThemeColors.radioUnCheckBgColor,
        ),
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.all(20.r),
              alignment: Alignment.center,
              child: Text(
                title,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  fontSize: 28.sp,
                  color: KqThemeColors.textBlack85,
                ),
              ),
            ),
            SizedBox(
              height: 8.r,
            ),
            reasonHeight == null
                ? Expanded(
                    child: _gridViewWidget(),
                  )
                : Container(
                    height: reasonHeight,
                    constraints: BoxConstraints(maxHeight: reasonHeight!),
                    child: _gridViewWidget(),
                  ),
            if (bottomChildWidget != null) bottomChildWidget!,
            if (isShowBottomBtn == true) _getBottomBtnList(),
          ],
        ),
      ),
    );
  }
}
